<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Constructor Forwarding</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../move.html" title="Chapter 22. Boost.Move">
<link rel="prev" href="move_and_containers.html" title="Containers and move semantics">
<link rel="next" href="move_return.html" title="Implicit Move when returning a local object">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="move_and_containers.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="move_return.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="move.construct_forwarding"></a><a class="link" href="construct_forwarding.html" title="Constructor Forwarding">Constructor Forwarding</a>
</h2></div></div></div>
<p>
      Consider writing a generic factory function that returns an object for a newly
      constructed generic type. Factory functions such as this are valuable for encapsulating
      and localizing the allocation of resources. Obviously, the factory function
      must accept exactly the same sets of arguments as the constructors of the type
      of objects constructed:
    </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">T</span><span class="special">*</span> <span class="identifier">factory_new</span><span class="special">()</span>
<span class="special">{</span>  <span class="keyword">return</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">();</span>  <span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">T</span><span class="special">*</span> <span class="identifier">factory_new</span><span class="special">(</span><span class="identifier">a1</span><span class="special">)</span>
<span class="special">{</span>  <span class="keyword">return</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">(</span><span class="identifier">a1</span><span class="special">);</span>  <span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">T</span><span class="special">*</span> <span class="identifier">factory_new</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">)</span>
<span class="special">{</span>  <span class="keyword">return</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">);</span>  <span class="special">}</span>
</pre>
<p>
      Unfortunately, in C++03 the much bigger issue with this approach is that the
      N-argument case would require 2^N overloads, immediately discounting this as
      a general solution. Fortunately, most constructors take arguments by value,
      by const-reference or by rvalue reference. If these limitations are accepted,
      the forwarding emulation of a N-argument case requires just N overloads. This
      library makes this emulation easy with the help of <code class="computeroutput"><span class="identifier">BOOST_FWD_REF</span></code>
      and <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward</span></code>:
    </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">move</span><span class="special">/</span><span class="identifier">utility_core</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">copyable_only_tester</span>
<span class="special">{</span>
   <span class="keyword">public</span><span class="special">:</span>
   <span class="identifier">copyable_only_tester</span><span class="special">()</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_only_tester()"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_only_tester</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">copyable_only_tester</span><span class="special">&amp;)</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_only_tester(const copyable_only_tester&amp;)"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_only_tester</span><span class="special">(</span><span class="keyword">int</span><span class="special">)</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_only_tester(int)"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_only_tester</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">)</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_only_tester(int, double)"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">class</span> <span class="identifier">copyable_movable_tester</span>
<span class="special">{</span>
   <span class="comment">// move semantics</span>
   <span class="identifier">BOOST_COPYABLE_AND_MOVABLE</span><span class="special">(</span><span class="identifier">copyable_movable_tester</span><span class="special">)</span>
   <span class="keyword">public</span><span class="special">:</span>

   <span class="identifier">copyable_movable_tester</span><span class="special">()</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_movable_tester()"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_movable_tester</span><span class="special">(</span><span class="keyword">int</span><span class="special">)</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_movable_tester(int)"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_movable_tester</span><span class="special">(</span><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">copyable_movable_tester</span><span class="special">))</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_movable_tester</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">copyable_movable_tester</span> <span class="special">&amp;)</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_movable_tester(const copyable_movable_tester &amp;)"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_movable_tester</span><span class="special">(</span><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">copyable_movable_tester</span><span class="special">),</span> <span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">copyable_movable_tester</span><span class="special">))</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>   <span class="special">}</span>

   <span class="identifier">copyable_movable_tester</span> <span class="special">&amp;</span><span class="keyword">operator</span><span class="special">=(</span><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">copyable_movable_tester</span><span class="special">))</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_movable_tester &amp; operator=(BOOST_RV_REF(copyable_movable_tester))"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
      <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>  <span class="special">}</span>

   <span class="identifier">copyable_movable_tester</span> <span class="special">&amp;</span><span class="keyword">operator</span><span class="special">=(</span><span class="identifier">BOOST_COPY_ASSIGN_REF</span><span class="special">(</span><span class="identifier">copyable_movable_tester</span><span class="special">))</span>
   <span class="special">{</span>  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"copyable_movable_tester &amp; operator=(BOOST_COPY_ASSIGN_REF(copyable_movable_tester))"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
      <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>  <span class="special">}</span>
<span class="special">};</span>

<span class="comment">//1 argument</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">MaybeMovable</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">MaybeRv</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">function_construct</span><span class="special">(</span><span class="identifier">BOOST_FWD_REF</span><span class="special">(</span><span class="identifier">MaybeRv</span><span class="special">)</span> <span class="identifier">x</span><span class="special">)</span>
<span class="special">{</span>  <span class="identifier">MaybeMovable</span> <span class="identifier">m</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">MaybeRv</span><span class="special">&gt;(</span><span class="identifier">x</span><span class="special">));</span>   <span class="special">}</span>

<span class="comment">//2 argument</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">MaybeMovable</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">MaybeRv</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">MaybeRv2</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">function_construct</span><span class="special">(</span><span class="identifier">BOOST_FWD_REF</span><span class="special">(</span><span class="identifier">MaybeRv</span><span class="special">)</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">BOOST_FWD_REF</span><span class="special">(</span><span class="identifier">MaybeRv2</span><span class="special">)</span> <span class="identifier">x2</span><span class="special">)</span>
<span class="special">{</span>  <span class="identifier">MaybeMovable</span> <span class="identifier">m</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">MaybeRv</span><span class="special">&gt;(</span><span class="identifier">x</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">MaybeRv2</span><span class="special">&gt;(</span><span class="identifier">x2</span><span class="special">));</span>  <span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
   <span class="identifier">copyable_movable_tester</span> <span class="identifier">m</span><span class="special">;</span>
   <span class="comment">//move constructor</span>
   <span class="identifier">function_construct</span><span class="special">&lt;</span><span class="identifier">copyable_movable_tester</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">m</span><span class="special">));</span>
   <span class="comment">//copy constructor</span>
   <span class="identifier">function_construct</span><span class="special">&lt;</span><span class="identifier">copyable_movable_tester</span><span class="special">&gt;(</span><span class="identifier">copyable_movable_tester</span><span class="special">());</span>
   <span class="comment">//two rvalue constructor</span>
   <span class="identifier">function_construct</span><span class="special">&lt;</span><span class="identifier">copyable_movable_tester</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">m</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">m</span><span class="special">));</span>

   <span class="identifier">copyable_only_tester</span> <span class="identifier">nm</span><span class="special">;</span>
   <span class="comment">//copy constructor (copyable_only_tester has no move ctor.)</span>
   <span class="identifier">function_construct</span><span class="special">&lt;</span><span class="identifier">copyable_only_tester</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">nm</span><span class="special">));</span>
   <span class="comment">//copy constructor</span>
   <span class="identifier">function_construct</span><span class="special">&lt;</span><span class="identifier">copyable_only_tester</span><span class="special">&gt;(</span><span class="identifier">nm</span><span class="special">);</span>
   <span class="comment">//int constructor</span>
   <span class="identifier">function_construct</span><span class="special">&lt;</span><span class="identifier">copyable_only_tester</span><span class="special">&gt;(</span><span class="keyword">int</span><span class="special">(</span><span class="number">0</span><span class="special">));</span>
   <span class="comment">//int, double constructor</span>
   <span class="identifier">function_construct</span><span class="special">&lt;</span><span class="identifier">copyable_only_tester</span><span class="special">&gt;(</span><span class="keyword">int</span><span class="special">(</span><span class="number">0</span><span class="special">),</span> <span class="keyword">double</span><span class="special">(</span><span class="number">0.0</span><span class="special">));</span>

   <span class="comment">//Output is:</span>
   <span class="comment">//copyable_movable_tester()</span>
   <span class="comment">//copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))</span>
   <span class="comment">//copyable_movable_tester()</span>
   <span class="comment">//copyable_movable_tester(const copyable_movable_tester &amp;)</span>
   <span class="comment">//copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))</span>
   <span class="comment">//copyable_only_tester()</span>
   <span class="comment">//copyable_only_tester(const copyable_only_tester&amp;)</span>
   <span class="comment">//copyable_only_tester(const copyable_only_tester&amp;)</span>
   <span class="comment">//copyable_only_tester(int)</span>
   <span class="comment">//copyable_only_tester(int, double)</span>
   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      Constructor forwarding comes in handy to implement placement insertion in containers
      with just N overloads if the implementor accepts the limitations of this type
      of forwarding for C++03 compilers. In compilers with rvalue references perfect
      forwarding is achieved.
    </p>
</div>
<div class="copyright-footer">Copyright © 2008-2024 Ion Gaztanaga<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="move_and_containers.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="move_return.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
